package com.externalizable.demo1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ExternalizableDemo {

	public static void main(String[] args) {
	      //Write Obj to file
	      User user = new User();
	      user.setName("hollis");
	      user.setAge(23);
	      try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("tempFile"))){
	          oos.writeObject(user);
	      } catch (IOException e) {
	          e.printStackTrace();
	      }

	      //Read Obj from file
	      File file = new File("tempFile");
	      try(ObjectInputStream ois =  new ObjectInputStream(new FileInputStream(file))){
	          User newInstance = (User) ois.readObject();
	          //output
	          System.out.println(newInstance);
	      } catch (IOException | ClassNotFoundException e ) {
	          e.printStackTrace();
	      }
	  }

}
